package com.guojiaming.shop.service;

import com.guojiaming.shop.bean.Member;
import com.guojiaming.shop.bean.MemberModify;
import com.guojiaming.shop.dao.MemberMapper;
import com.guojiaming.shop.exception.DataNotFindException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author vivo
 */
@Service
@Transactional(rollbackFor = {Exception.class})
public class MemberService {
    @Resource
    private MemberMapper memberMapper;


    public List<Member> findAll() {
        return memberMapper.selectAll();
    }

    public Member addMember(Member member) {
        memberMapper.saveMember(member);
        return member;
    }

    public Integer deleteMember(Integer memberId) {
        if (memberMapper.deleteMember(memberId) <= 0) {
            throw new DataNotFindException();
        }
        return memberId;
    }

    public Member updateMember(MemberModify memberModify) {
        if (memberModify.getMemberId() == null) {
            throw new DataNotFindException("必须指定memberId值");
        }
        if (memberMapper.updateMember(Member.getInstance(memberModify)) <= 0) {
            throw new DataNotFindException();
        }
        return memberMapper.selectMember(memberModify.getMemberId());
    }

    public Member selectMember(Integer memberId) {
        Member member = memberMapper.selectMember(memberId);
        if (member == null) {
            throw new DataNotFindException();
        }
        return member;
    }
}
